package kr.co.tomatosystem.pde.tools.internal.nature;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.tomatosystem.pde.tools.Constants;
import kr.co.tomatosystem.pde.tools.internal.build.ResourceAccessorBuilder;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

public class ResourceAccessorNature implements IProjectNature {
	public static final String NATURE_ID = "kr.co.tomatosystem.pde.tools.resourceAccessor";

	public static void installNature(IProject project, IProgressMonitor monitor) throws CoreException {
		IProjectDescription description = project.getDescription();
		List<String> natures = new ArrayList<String>();
		natures.addAll(Arrays.asList(description.getNatureIds()));

		if (!natures.contains(NATURE_ID)) {
			natures.add(NATURE_ID);
			description.setNatureIds(natures.toArray(new String[natures.size()]));
			project.setDescription(description, monitor);
		}
	}

	public static void uninstallNature(IProject project, IProgressMonitor monitor) throws CoreException {
		IProjectDescription description = project.getDescription();
		List<String> natures = new ArrayList<String>();
		natures.addAll(Arrays.asList(description.getNatureIds()));

		if (natures.contains(NATURE_ID)) {
			natures.remove(NATURE_ID);
			description.setNatureIds(natures.toArray(new String[natures.size()]));
			project.setDescription(description, monitor);
		}
	}

	private IProject project;

	@Override
	public void configure() throws CoreException {
		IProjectDescription description = this.project.getDescription();
		ArrayList<ICommand> commands = new ArrayList<ICommand>();
		commands.addAll(Arrays.asList(description.getBuildSpec()));

		ICommand newCommand = description.newCommand();
		newCommand.setArguments(getDefaultBuilderMap());
		newCommand.setBuilderName(ResourceAccessorBuilder.BUILDER_ID);

		commands.add(newCommand);

		description.setBuildSpec(commands.toArray(new ICommand[commands.size()]));
		this.project.setDescription(description, new NullProgressMonitor());

	}

	private Map<String, String> getDefaultBuilderMap() {
		Map<String, String> argMap = new HashMap<String, String>();
		argMap.put(Constants.BUILDER_ARG__KEY_SRC, "src");
		argMap.put(Constants.BUILDER_ARG__KEY_MONITORED_RESOURCE, "icons");
		argMap.put(Constants.BUILDER_ARG__KEY_PACKAGE, "shared");
		argMap.put(Constants.BUILDER_ARG__ACCESSOR_CLASS_NAME, "Images");
		return argMap;

	}

	@Override
	public void deconfigure() throws CoreException {
		IProjectDescription description = this.project.getDescription();
		ArrayList<ICommand> commands = new ArrayList<ICommand>();
		commands.addAll(Arrays.asList(description.getBuildSpec()));

		for (ICommand each : commands.toArray(new ICommand[commands.size()])) {
			if (each.getBuilderName().equals(ResourceAccessorBuilder.BUILDER_ID)) {
				commands.remove(each);
			}
		}

		description.setBuildSpec(commands.toArray(new ICommand[commands.size()]));
		this.project.setDescription(description, new NullProgressMonitor());
	}

	@Override
	public IProject getProject() {
		return this.project;
	}

	@Override
	public void setProject(IProject project) {
		this.project = project;
	}
}
